"""
通用方法
"""
from flask import Blueprint, request, jsonify
import server.database.sql.graph.describe as db_describe
from server.utils.formatData import format_data
from server.utils.handleFile import file_to_base64
from server.utils.formatData import format_request_params

route_common = Blueprint('', __name__)


# 信息获取
# 获取节点和边等信息
@route_common.route('/get_img', methods=['GET'])
def get_img():
    if request.method == 'GET':
        request.args = format_request_params(request.args)
        paths = request.args.get('paths')
        quality = float(request.args.get('quality'))
        quality = 1 if quality is None else quality
        res = []
        if paths is not None:
            for path in paths:
                res.append(file_to_base64(path, quality))
        return res


# 根据gids集合获取描述信息集合
def get_describes_by_gids(info_list):
    res = []
    for item in info_list:
        # 查询数据库，整合信息
        gid = item['gid']
        uid = item['uid']
        data = db_describe.get_describe(gid)
        if data is None:
            res.append('null')
            continue
        data['favorite'] = False # 获取收藏信息
        # 调整格式
        data = format_data(data)

        if data['otype'] == 1:
            data['time'] = data['utime']
        elif data['otype'] == 2:
            data['time'] = data['etime']
        elif data['otype'] == 0:
            data['time'] = data['ctime']
        # 处理图片
        if data['img'] is None or len(data['img']) == 0:
            data['img'] = []
        # 即时加载图片
        # else:
        #     i = 0
        #     for file_path in data['img']:
        #         base64 = file_to_base64(file_path, 0.2)
        #         data['img'][i] = base64
        #         i += 1
        # 后加载图片
        else:
            i = 0
            data['img_path'] = data['img'].copy()
            for file_path in data['img']:
                data['img'][i] = ''
                i += 1
        res.append(data)

    return res

